
bt.module({builder: function(bt) {
    var j$ = jQuery;

    var _initAutoLoadMoreEvent = function() {
        var body = j$("body.new-header");
        j$(window).scroll(function() {
            if (j$("#notifications-wrapper").attr("data-load-flag") == "1") {
                return;
            }
            if((j$(window).scrollTop() + j$(window).height()) + 10 >= body.height()) {
                j$("#notifications-wrapper").attr("data-load-flag", "1");
                var load_more = j$("#load-more.notifications");
                if (load_more.data("count") === undefined) return;
                load_more.show();
                j$('#ajax-more').css("visibility", "visible");
                j$.ajax({
                    url: load_more.data("url"),
                    data: "&count=" + parseInt(load_more.data("count")),
                    success: function(html) {
                        if(html) {
                            j$('#notifications-wrapper').replaceWith(html);
                            _initNotificationEvents();
                            _ellipsis();
                            j$(window).resize();
                        } else {
                            j$("#load-more.notifications").slideUp();
                        }
                    }
                }).done(function() {
                        j$("#load-more.notifications").hide();
                        j$("#notifications-wrapper").attr("data-load-flag", "0");
                    });
            }
        });
    };

    var _ellipsis = function() {
        j$(".trip-name").each(function() {
            j$(this).ThreeDots({max_rows: 1});
        });
    };

    var _changeFloatAlarm = function(d_noti, d_msg) {
        var float_alarm = j$(".new-header .float-alarm");
        var num_noti = parseInt(j$(".user-menu .notification span.new-count").text()) || 0;
        var num_msg = parseInt(j$(".user-menu .message span.new-count").text()) || 0;
        d_noti = d_noti == "all"? -num_noti: (d_noti || 0);
        d_msg = d_msg == "all"? -num_msg: (d_msg || 0);
        num_noti += d_noti;
        num_msg += d_msg;

        if(num_noti <= 0 && num_msg <= 0) {
            j$(".user-menu .notification span.new-count").remove();
            j$(".user-menu .message span.new-count").remove();
            float_alarm.slideUp(function() {
                j$(this).remove();
            });
            return;
        }
        if(num_noti <= 0) {
            float_alarm.find("a.notification").remove();
            j$(".user-menu .notification span.new-count").remove();
            float_alarm.find("span.sep").remove();
        } else {
            float_alarm.find("a.notification span").text(num_noti);
            j$(".user-menu .notification span.new-count").text(num_noti);
        }
        if(num_msg <= 0) {
            float_alarm.find("a.message").remove();
            j$(".user-menu .message span.new-count").remove();
            float_alarm.find("span.sep").remove();
        } else {
            float_alarm.find("a.message span").text(num_msg);
            j$(".user-menu .message span.new-count").text(num_msg);
        }
    };

    var _initNotificationEvents = function() {
        j$('a.close').click(function() {
            var notification = j$(this).parent();
            notification.css('opacity', 0.3);
            notification.children('img.ajax_indicator').css('display', 'block');
            j$.ajax({
                url: j$(this).attr('href'),
                success: function(msg){
                    notification.slideUp('slow', function() { 
                        notification.remove();
                        if (j$("#load-more").length == 0 && j$('#notifications').children().length == 0) {
                            j$('#notifications-wrapper .hint').slideDown();
                            j$(".new-notifications").hide();
                            j$('.clear-all').remove();
                            j$('#load-more').remove();
                        } else {
                        }
                        _changeFloatAlarm(-1, 0);
                    });
                    j$(window).resize();
                }
            });
            return false;
        });

        j$("a.confirm-friend, a.reject-friend").click(function() {
            var notification = j$(this).parents("li.add-friend");
            notification.find("btn-friend").css('opacity', 0.3);
            notification.children('img.ajax_indicator').css('display', 'block');
            j$.ajax({
                url: j$(this).attr('href'),
                success: function(resp){
                    if(!resp.error) {
                        notification.slideUp('slow', function() { 
                            notification.remove();
                            if (j$("#load-more").length == 0 && j$('#notifications').children().length == 0) {
                                j$('#notifications-wrapper .hint').slideDown();
                                j$(".new-notifications").hide();
                                j$('.clear-all').remove();
                                j$('#load-more').remove();
                            } else {
                            }
                        });
                        _changeFloatAlarm(-1, 0);
                    } else {
                        alert(resp.error);
                        notification.find("btn-friend").css('opacity', 1);
                    }
                    j$(window).resize();
                }
            });
            return false;
        });
    };

    j$(document).ready(function(){
        _initAutoLoadMoreEvent();
        _initNotificationEvents();
        _ellipsis();

        j$('.clear-all').click(function() {
            var rtn = confirm("确认全部清除吗？");
            if(!rtn) return null;
            j$('#notifications').css('opacity', 0.3);
             j$.ajax({
                url: '/notifications/clear/',
                success: function(msg){
                    j$('#notifications').slideUp('slow', function() { 
                        j$('#notifications-wrapper .hint').slideDown();
                        j$(".new-notifications").hide();
                        j$('.clear-all').remove();
                        j$('#load-more').remove();
                        _changeFloatAlarm("all", 0);
                    });
                    j$(window).resize();
                }
            });
            return false;
        });

    });
}});
